package com.the_ring.chapter11.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

public class SingleTouchView extends View {

    private Paint mPathPaint = new Paint();
    private Path mPath = new Path();
    private PointF mLastPos, mBeginPos, mEndPos;
    private int dip_17;

    public SingleTouchView(Context context) {
        this(context, null);
    }

    public SingleTouchView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        dip_17 = dip2px(context, 17);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawPath(mPath, mPathPaint);
        if (mBeginPos != null) {
            canvas.drawCircle(mBeginPos.x, mBeginPos.y, 10, mPathPaint);
            canvas.drawText("起点", mBeginPos.x - dip_17, mBeginPos.y + dip_17, mPathPaint);
        }

        if (mEndPos != null) {
            canvas.drawCircle(mEndPos.x, mEndPos.y, 10, mPathPaint);
            canvas.drawText("起点", mEndPos.x - dip_17, mEndPos.y + dip_17, mPathPaint);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mPath.reset();
                mPath.moveTo(event.getX(), event.getY());
                mBeginPos = new PointF(event.getX(), event.getY());
                mEndPos = null;
                break;
            case MotionEvent.ACTION_MOVE:
                mPath.quadTo(mLastPos.x, mLastPos.y, event.getX(), event.getY());
                break;
            case MotionEvent.ACTION_UP:
                mEndPos = new PointF(event.getX(), event.getY());
                mPath.quadTo(mLastPos.x, mLastPos.y, event.getX(), event.getY());
                if (mListener != null) {
                    mListener.onFlipFinish(mBeginPos, mEndPos);
                }
                break;
        }
        mLastPos = new PointF(event.getX(), event.getY());
        postInvalidate();
        return true;
    }

    // 根据是手机的分辨率从 dp 的单位转化为 px
    public int dip2px(Context context, float dpValue) {
        // 获取手机像素密度（1dp 对应几个 px）
        float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f); // 四舍五入取整
    }

    private FlipListener mListener;

    public void setListener(FlipListener mListener) {
        this.mListener = mListener;
    }

    public interface FlipListener {
        void onFlipFinish(PointF beginPos, PointF endPos);
        /**
         * float offsetX = Math.abs(endPos.x - beginPos.x);
         * float offsetY = Math.abs(endPos.y - beginPos.y);
         * if (offsetX > offsetY) {
         *     (endPos.x - beginPos.x > 0) ? "向右" : "向左";
         * } else {
         *     (endPos.y - beginPos.y > 0) ? "向下" : "向上";
         * }
         */
    }
}
